/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package testing;

import clasificador.genetico.PoliticaCruce;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import utils.Couple;
import utils.Weighted;

/**
 *
 * @author Simply
 */
public class CruceString implements PoliticaCruce<ArrayList<Character>> {

    @Override
    public List<ArrayList<Character>> cruzar(Couple<Weighted<ArrayList<Character>>> pareja) {
        Random r = new Random();
        ArrayList<Character> s1 = pareja.l().l();
        ArrayList<Character> s2 = pareja.r().l();

        if( s1.isEmpty() && s2.isEmpty() ) {
            return new ArrayList<>(Arrays.asList(s1, s2, new ArrayList<>(s1), new ArrayList<>(s2)));
        }

        if( s1.isEmpty() ) {
            return cruzar(new Couple(pareja.r(), pareja.r()));
        } else if( s2.isEmpty() ) {
            int indice1 = r.nextInt(s1.size());
            return new ArrayList<>(Arrays.asList(s1, s2,
                    new ArrayList<>(s1.subList(0, indice1)),
                    new ArrayList<>(s1.subList(indice1+1, s1.size()))));
        } else {
            int indice1 = r.nextInt(s1.size());
            int indice2 = r.nextInt(s2.size());

            ArrayList<Character> left = new ArrayList(s1.subList(0, indice1));
            left.addAll(s2.subList(indice2 + 1, s2.size()));
            ArrayList<Character> right = new ArrayList(s2.subList(0, indice2));
            right.addAll(s1.subList(indice1 + 1, s1.size()));

            return new ArrayList<>(Arrays.asList(s1, s2, left, right));
        }
    }
    
}
